package com.wyq.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.wyq.bean.Company;
import com.wyq.service.CompanyService;
import com.wyq.service.ShowInfoService;
import com.wyq.util.PropertiesUtil;

public class PreCompanyAction extends ActionSupport {
	private CompanyService companyService;
	private ShowInfoService showInfoService;
	
	private String source;//展会渠道
	private Map showInfo;//展会信息
	//验证
	private String name;
	private String value;
	private List companyNameList;
	//注册
	private Company company;
	
	//验证展商名称是否存在
	public String companyNameIsExist() {
		try{
			
			//获取展会信息
			String show_id = getShowInfoId();
			
			companyNameList = companyService.companyListByName(name, value,show_id);
			
			return "validateSuccess";
		}catch(Exception e){
			e.printStackTrace();
		}
		return "validateSuccess";
	}
	
	//展商注册
	public String register(){
		try {
			//获取展会信息
			String show_id = getShowInfoId();
			
			List maxIDList = companyService.getCompanyMaxID();
			int id = 0;
			if(maxIDList.size() > 0){
				Map idMap = (Map)maxIDList.get(0);
				if(null != idMap.get("id")){
					id = Integer.parseInt(idMap.get("id").toString()) + 1;
				}else{
					id = 1;
				}
			}
			company.setId(id);
			//TODO:展会
			//company.setType(type);
			//当前时间
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date = new Date();
			company.setRegisterDate(sdf.format(date).toString());
			company.setShow_id(show_id);
			//audience.setAudience_registerDate(sdf.format(date).toString());
			companyService.saveCompany(company);
	    	return "registerSuccess";
		} catch (Exception e) {
			e.printStackTrace();
			return "registerFail";
		}
		
	}
	
	//获取展会信息
	private String getShowInfoId() throws Exception{
		//获取展会信息
		String show_id = "";
		List<Map> showInfos = showInfoService.getShowInfoByTypeOrUrlChannel(null, source);
		if(null != showInfos && showInfos.size()>0){
			Map domain = (Map)showInfos.get(0);
			showInfo = domain;
			show_id = domain.get("id")!= null?domain.get("id").toString():"";
		}
		return show_id;
	}
	
	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setValue(String value) {
		this.value = value;
	}
	
	public void setCompanyNameList(List companyNameList) {
		this.companyNameList = companyNameList;
	}
	public List getCompanyNameList() {
		return companyNameList;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public Company getCompany() {
		return company;
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public void setShowInfoService(ShowInfoService showInfoService) {
		this.showInfoService = showInfoService;
	}
	public Map getShowInfo() {
		return showInfo;
	}
	
}
